package com.example.administrator.fangbook.dataLibrary;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.example.administrator.fangbook.dataLibrary.book.ShelfItemBook;
import com.example.administrator.fangbook.utils.LogUtil;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.io.File;
import java.io.InputStream;
import java.sql.SQLException;

public class DataHelp extends OrmLiteSqliteOpenHelper {
    public static final String DATABASE_NAME = "huitailang_reader.db";
    private static final int DATABASE_VERSION = 1;
    private Dao<ShelfItemBook, String> mShelfBookDao;

    public DataHelp(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
        try {
            TableUtils.createTableIfNotExists(connectionSource, ShelfItemBook.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int i, int i1) {
        LogUtil.e("数据库", "oldVersion:" + i + " newVersion:" + i1);
    }


    public Dao<ShelfItemBook, String> getShelfBookDao() {
        if (mShelfBookDao == null) {
            try {
                mShelfBookDao = getDao(ShelfItemBook.class);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return mShelfBookDao;
    }
}
